package com.bosicen.crm.web.controller.sysmanage;


import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bosicen.crm.dao.entity.ProductSery;
import com.bosicen.crm.dao.entity.User;
import com.bosicen.crm.service.sysmanage.ProductSeryServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.bosicen.crm.tools.Page;
import com.google.gson.Gson;

/***
 * 商品系列
 * @author niyong
 *
 */
@Controller
@RequestMapping("/basicinfo")
public class ProductSeryController {
	
	@Autowired
	public ProductSeryServiceImpl productSeryService;
	
	/****
	 * 新增
	 * @return
	 */
	@RequestMapping(value="/addProductsery/{type}")
	public String addProductSery(HttpServletRequest request,@PathVariable("type") Integer type){
		String id = request.getParameter("id");
		ProductSery productSery = null;
		try{
			if(type != 0 && StringUtils.isNotBlank(id)){
				productSery = productSeryService.findProductSeryById(AppUtil.ObjectToInt(id));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		request.setAttribute("productSery",productSery);
		return "sysmanage/addProductSery";
	}
	
	/****
	 * 查询产品系列列表
	 * @return
	 */
	@RequestMapping(value="/findProductsery")
	public String findProductSery(HttpServletRequest request){
		User user=(User) request.getSession().getAttribute("userinfo");
		Page page = new Page();
		page.setEveryPage(10);
		try{
			page.setCurrentPage(AppUtil.ObjectToInt(request.getParameter("pageno")));
			page = productSeryService.findProductSery4Page(page,user.getCompanyid());
			request.setAttribute("page", page);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "sysmanage/ProductSery_list";
	}
	
	/****
	 * 新增/修改 产品系列信息
	 * @param ps
	 * @param request
	 * @param type   0：删除   1: 新增/修改
	 */
	@RequestMapping(value="/cudProductsery/{type}")
	public void cudProductSery(ProductSery ps,HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		Gson gson=new Gson();
		boolean flag=false;
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			if(user == null) return ;
			ps.setCompanyid(user.getCompanyid());
			switch(type){
				case 0: flag = productSeryService.delProductSery(ps);break;
				case 1: flag = productSeryService.saveOrUpdateProduct(ps);break;
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	/***
	 * 通过类型查询产品系列
	 * @param request
	 * @param type
	 * @param writer
	 */
	@RequestMapping(value="/findProductSeryList/{type}")
	public void findProductSeryList(HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		 List<ProductSery>  list=null;
		 Gson gson=new Gson();
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			ProductSery sery =new ProductSery();
			sery.setCompanyid(user.getCompanyid());
			sery.setPid(type);
			list=this.productSeryService.findProductSeryList(sery);
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				writer.write(gson.toJson(list));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
}
